package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* SecurityFlagView
* */
class SecurityFlagView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            strokeCap = Paint.Cap.ROUND
            //style = Paint.Style.STROKE
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_10DC3E)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_dn_black_white)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            mPath.apply {
                reset()
                moveTo(cx * 90 / 100, -cy * 70 / 100)
                lineTo(cx * 90 / 100, cy * 40 / 100)
                quadTo(cx * 50 / 100, cy * 80 / 100, 0F, cy * 95 / 100)
                quadTo(-cx * 50 / 100, cy * 80 / 100, -cx * 90 / 100, cy * 40 / 100)
                lineTo(-cx * 90 / 100, -cy * 70 / 100)
                quadTo(-cx * 20 / 100, -cy * 70 / 100, 0F, -cy * 95 / 100)
                quadTo(cx * 20 / 100, -cy * 70 / 100, cx * 90 / 100, -cy * 70 / 100)
            }
            drawPath(mPath, mPaint0)

            mPaint1.strokeWidth = (cx / 6).coerceAtLeast(minStrokeWidth)
            save()
            translate(-cy * 1 / 5, cy * 1 / 5)
            mPath.apply {
                reset()
                moveTo(-cx * 1 / 5, -cy * 1 / 5)
                lineTo(0F, 0F)
                lineTo(cx * 3 / 5, -cy * 3 / 5)
            }
            drawPath(mPath, mPaint1)
            restore()

            restore()
        }
    }
}